home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / itc / cc1.spur / symout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  32.0 KB  |  1,217 lines

  1. /* Output GDB-format symbol table information from GNU compiler.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "tree.h"
  24. #include "symseg.h"
  25. #include "rtl.h"
  26. #include "gdbfiles.h"
  27. #include <stdio.h>
  28. #undef NULL
  29. #include "stddef.h"
  30. #include "alloca.h"
  31.  
  32. /* Get N_SO from stab.h if we can expect the file to exist.  */
  33. #ifdef DBX_DEBUGGING_INFO
  34. #include <stab.h>
  35. #endif
  36.  
  37. /* .stabs code for source file name.  */
  38. #ifndef N_SO
  39. #define    N_SO 0x64
  40. #endif
  41.  
  42. /* Unix maximum on file name length.  Needed for getwd.  */
  43. #define MAXNAMLEN 1024
  44.  
  45. /* Get the number to output for a reference to type TYPE.  */
  46. #define TYPE_OUTPUT_ADDRESS(TYPE) \
  47.   TYPE_SYMTAB_ADDRESS (TYPE_MAIN_VARIANT (TYPE))
  48.  
  49. /* Stream for writing symbol table file.  */
  50. static FILE *symfile;
  51.  
  52. /* Name of symbol table file.  */
  53. static char *symfile_name;
  54.  
  55. /* Stream for writing to assembler file.  */
  56. static FILE *asmfile;
  57.  
  58. /* Address for allocating space in symbol table file.
  59.    Changes in this variable are paired globally with writes to symfile,
  60.    but often we allocate many structures, advancing next_address,
  61.    before writing any of them.  */
  62. static int next_address;
  63.  
  64. /* Chain recording all the types that have been output,
  65.    giving the address-in-the-symseg of each one.  */
  66.  
  67. struct typevec_elt
  68. {
  69.   int address;
  70.   struct typevec_elt *next;
  71. };
  72.  
  73. static struct typevec_elt *typevec;
  74.  
  75. /* Number of types recorded so far in the chain.  */
  76.  
  77. static int total_types;
  78.  
  79. /* Lists of types to which forward references have been made.
  80.    Separate lists for temporary and permanent types.  */
  81.  
  82. static tree temporary_fwd_refs;
  83. static tree permanent_fwd_refs;
  84.  
  85. /* `blockvec' is a chain recording all the symbol-blocks that have been output,
  86.    giving the address-in-the-symseg of each one.  */
  87.  
  88. struct blockvec_elt
  89. {
  90.   int address;
  91.   struct blockvec_elt *next;
  92. };
  93.  
  94. static struct blockvec_elt *blockvec;
  95.  
  96. /* Number of blocks recorded so far in the chain.  */
  97.  
  98. static int total_blocks;
  99.  
  100. static void symout_range_bounds ();
  101. static void symout_array_domain ();
  102. static void symout_record_fields ();
  103. static void symout_enum_values ();
  104. static void symout_record_field_names ();
  105. static void symout_enum_value_names ();
  106. static int subrange_p ();
  107. static void symout_strings_skip ();
  108. static void symout_strings_print ();
  109.  
  110. /* At the beginning of compilation, start writing the symbol table.
  111.    Initialize the type and block chain.
  112.    Also open and initialize the symseg file.  */
  113.  
  114. void
  115. symout_init (filename, asm_file, sourcename)
  116.      char *filename;
  117.      FILE *asm_file;
  118.      char *sourcename;
  119. {
  120.   struct symbol_root buffer;
  121.  
  122. #ifdef VMS
  123.   fatal ("Cannot write GDB debugging format on VMS");
  124. #endif
  125.  
  126.   asmfile = asm_file;
  127.   fprintf (asmfile, ".text 0\n.gdbbeg 0\n.gdbbeg 1\n");
  128.   fprintf (asmfile,
  129.        "Ltext:\t.stabs \"%s\",%d,0,0,Ltext\n",
  130.        sourcename, N_SO);
  131.   fprintf (asmfile, ".data 0\nLdata:\n");
  132.   ASM_OUTPUT_LOCAL (asmfile, "Lbss", 0);
  133.   fprintf (asmfile, ".gdbsym Ldata,%d\n",
  134.        (char *) &buffer.databeg - (char *) &buffer);
  135.   fprintf (asmfile, ".gdbsym Lbss,%d\n",
  136.        (char *) &buffer.bssbeg - (char *) &buffer);
  137.  
  138.   symfile = fopen (filename, "w");
  139.   if (symfile == 0)
  140.     pfatal_with_name (symfile);
  141.   symfile_name = (char *) malloc (strlen (filename) + 1);
  142.   strcpy (symfile_name, filename);
  143.  
  144.   typevec = 0;
  145.   blockvec = 0;
  146.   total_types = 0;
  147.   total_blocks = 0;
  148.  
  149.   permanent_fwd_refs = 0;
  150.   temporary_fwd_refs = 0;
  151.  
  152.   bzero (&buffer, sizeof buffer);
  153.   fwrite (&buffer, sizeof buffer, 1, symfile);
  154.  
  155.   next_address = sizeof buffer;
  156. }
  157.  
  158. /* Functions for outputting strings into the symbol table.
  159.    The string to be output is effectively the concatenation of
  160.    the two strings P1 and P2.  Their lengths are given as S1 and S2.
  161.    If P1 or P2 is zero, that string is not used.
  162.  
  163.    A null character is output to terminate the string,
  164.    and it is followed by more nulls as padding to a word boundary.  */
  165.  
  166. static void
  167. symout_strings (p1, s1, p2, s2)
  168.      char *p1;
  169.      int s1;
  170.      char *p2;
  171.      int s2;
  172. {
  173.   symout_strings_print (p1, s1, p2, s2);
  174.   symout_strings_skip (p1, s1, p2, s2);
  175. }
  176.  
  177. /* Like symout_strings but only output; do not update next_address.  */
  178.  
  179. static void
  180. symout_strings_print (p1, s1, p2, s2)
  181.      char *p1;
  182.      int s1;
  183.      char *p2;
  184.      int s2;
  185. {
  186.   register int total;
  187.  
  188.   if (p1 && s1 == 0)
  189.     s1 = strlen (p1);
  190.   if (p2 && s2 == 0)
  191.     s2 = strlen (p2);
  192.  
  193.   if (p1)
  194.     fwrite (p1, s1, 1, symfile);
  195.   if (p2)
  196.     fwrite (p2, s2, 1, symfile);
  197.   putc (0, symfile);
  198.  
  199.   total = s1 + s2 + 1;
  200.   while (total % sizeof (int))
  201.     {
  202.       putc (0, symfile);
  203.       total++;
  204.     }
  205. }
  206.  
  207. /* Like symout_strings but just update next_address; do not output.  */
  208.  
  209. static void
  210. symout_strings_skip (p1, s1, p2, s2)
  211.      char *p1;
  212.      int s1;
  213.      char *p2;
  214.      int s2;
  215. {
  216.   register int total;
  217.  
  218.   if (p1 && s1 == 0)
  219.     s1 = strlen (p1);
  220.   if (p2 && s2 == 0)
  221.     s2 = strlen (p2);
  222.  
  223.   total = s1 + s2 + 1;
  224.   while (total % sizeof (int))
  225.     total++;
  226.  
  227.   next_address += total;
  228. }
  229.  
  230. /* Call here to output a chain of types.
  231.    After each function, this is done first for the chain of permanent types
  232.    made during the function, and then for the chain of temporary types.
  233.    This must be done before outputting the symbols and blocks of the function.
  234.  
  235.    At the end of compilation, this is done for all the permanent types
  236.    made since the last function.
  237.  
  238.    Each permanent type is done once, at the beginning of the next function,
  239.    or at the end of the compilation if no functions follow.
  240.    Once a type has been processed here, its TYPE_SYMTAB_ADDRESS remains
  241.    set up.  */
  242.  
  243. void
  244. symout_types (types)
  245.      tree types;
  246. {
  247.   struct typerec
  248.   {
  249.     int number;
  250.     int address;
  251.     int nfields;
  252.     int fields_address;
  253.     int name_address;
  254.     char *name;
  255.     char *name_prefix;
  256.   };
  257.  
  258.   register int n_types, i;
  259.   register struct typerec *records;
  260.   register tree next;
  261.   struct type buffer;
  262.   int this_run_address = next_address;
  263.  
  264.   /* Count the number of types to be handled here.  */
  265.  
  266.   for (next = types, n_types = 0;
  267.        next;
  268.        next = TREE_CHAIN (next), n_types++);
  269.  
  270.   records = (struct typerec *) alloca (n_types * sizeof (struct typerec));
  271.  
  272.   /* Compute the amount of space each type needs, updating next_address
  273.      and storing the address of the data for each type.  */
  274.  
  275.   for (next = types, i = 0;
  276.        next;
  277.        next = TREE_CHAIN (next), i++)
  278.     {
  279.       register struct typevec_elt *velt
  280.     = (struct typevec_elt *) xmalloc (sizeof (struct typevec_elt));
  281.       velt->next = typevec;
  282.       typevec = velt;
  283.  
  284.       total_types++;
  285.  
  286.       if (TYPE_NAME (next))
  287.     {
  288.       records[i].name_address = next_address;
  289.  
  290.       if (TREE_CODE (TYPE_NAME (next)) == IDENTIFIER_NODE)
  291.         {
  292.           records[i].name = IDENTIFIER_POINTER (TYPE_NAME (next));
  293.           switch (TREE_CODE (next))
  294.         {
  295.         case RECORD_TYPE:
  296.           records[i].name_prefix = "struct ";
  297.           break;
  298.  
  299.         case UNION_TYPE:
  300.           records[i].name_prefix = "union ";
  301.           break;
  302.  
  303.         case ENUMERAL_TYPE:
  304.           records[i].name_prefix = "enum ";
  305.           break;
  306.         }
  307.         }
  308.       else
  309.         {
  310.           records[i].name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (next)));
  311.           records[i].name_prefix = 0;
  312.         }
  313.       symout_strings_skip (records[i].name_prefix, 0,
  314.                    records[i].name, 0);
  315.  
  316.     }
  317.       else
  318.     {
  319.       records[i].name = 0;
  320.       records[i].name_address = 0;
  321.       records[i].name_prefix = 0;
  322.     }
  323.  
  324.       /* If this type was forward-referenced from a previous call
  325.      to symout_types, store this type's address into the reference.  */
  326.       if (TYPE_POINTER_TO (next) != 0
  327.       && TYPE_SYMTAB_ADDRESS (TYPE_POINTER_TO (next)) != 0
  328.       && TYPE_SYMTAB_ADDRESS (TYPE_POINTER_TO (next)) < this_run_address)
  329.     {
  330.       int pos = ftell (symfile);
  331.       int myaddr = next_address;
  332.       fflush (symfile);
  333.       fseek (symfile,
  334.          (TYPE_SYMTAB_ADDRESS (TYPE_POINTER_TO (next))
  335.           + offsetof (struct type, target_type)),
  336.          0);
  337.       fwrite (&myaddr, sizeof (int), 1, symfile);
  338.       fflush (symfile);
  339.       fseek (symfile, pos, 0);
  340.     }
  341.  
  342.       records[i].address = next_address;
  343.       TYPE_SYMTAB_ADDRESS (next) = next_address;
  344.       velt->address = next_address;
  345.       next_address += sizeof (struct type);
  346.       records[i].nfields = 0;
  347.       records[i].fields_address = 0;
  348.       switch (TREE_CODE (next))
  349.     {
  350.     case ARRAY_TYPE:
  351.       records[i].nfields
  352.         = (TYPE_DOMAIN(next)
  353.            ? ! integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (next)))
  354.            : 0 );
  355.       break;
  356.  
  357.     case INTEGER_TYPE:
  358.       if (subrange_p (next))
  359.         buffer.nfields = 2;
  360.       break;
  361.  
  362.     case RECORD_TYPE:
  363.     case UNION_TYPE:
  364.     case ENUMERAL_TYPE:
  365.       records[i].nfields = list_length (TYPE_FIELDS (next));
  366.     }
  367.       if (records[i].nfields)
  368.     records[i].fields_address = next_address;
  369.       next_address += records[i].nfields * sizeof (struct field);
  370.     }
  371.  
  372.   /* Now write the data whose space we have assigned.
  373.      First fill the data into BUFFER, then write BUFFER.  */
  374.  
  375.   for (next = types, i = 0;
  376.        next;
  377.        next = TREE_CHAIN (next), i++)
  378.     {
  379.       if (records[i].name)
  380.     symout_strings_print (records[i].name_prefix, 0,
  381.                   records[i].name, 0);
  382.  
  383.       if (TREE_TYPE (next) != 0 && TYPE_OUTPUT_ADDRESS (TREE_TYPE (next)) == 0)
  384.     {
  385.       /* We are making a forward-reference to our target type.
  386.          Make a list of all of these.  */
  387.       if (TREE_PERMANENT (next))
  388.         permanent_fwd_refs
  389.           = tree_cons (TREE_TYPE (next), 0, permanent_fwd_refs);
  390.       else
  391.         temporary_fwd_refs
  392.           = tree_cons (TREE_TYPE (next), 0, temporary_fwd_refs);
  393.     }
  394.  
  395.       if (TYPE_SIZE (next) == 0)
  396.     buffer.length = 0;
  397.       else
  398.     buffer.length
  399.       = (TREE_INT_CST_LOW (TYPE_SIZE (next))
  400.          * TYPE_SIZE_UNIT (next) / BITS_PER_UNIT);
  401.  
  402.       buffer.name = (char *) records[i].name_address;
  403.       buffer.target_type = (struct type *) (TREE_TYPE (next) ? TYPE_OUTPUT_ADDRESS (TREE_TYPE (next)) : 0);
  404.  
  405.       buffer.pointer_type = 0;
  406.       buffer.function_type = 0;
  407.       buffer.flags
  408.     = ((TREE_CODE (next) == INTEGER_TYPE || TREE_CODE (next) == ENUMERAL_TYPE)
  409.        && TREE_UNSIGNED (next))
  410.       ? TYPE_FLAG_UNSIGNED : 0;
  411.       buffer.nfields = records[i].nfields;
  412.       buffer.fields = (struct field *) records[i].fields_address;
  413.  
  414.       switch (TREE_CODE (next))
  415.     {
  416.     case INTEGER_TYPE:
  417.       buffer.code = TYPE_CODE_INT;
  418.       if (buffer.nfields)
  419.         buffer.code = TYPE_CODE_RANGE;
  420.       break;
  421.  
  422.     case REAL_TYPE:
  423.       buffer.code = TYPE_CODE_FLT;
  424.       break;
  425.  
  426.     case VOID_TYPE:
  427.       buffer.code = TYPE_CODE_VOID;
  428.       break;
  429.  
  430.     case POINTER_TYPE:
  431.       buffer.code = TYPE_CODE_PTR;
  432.       break;
  433.  
  434.     case ARRAY_TYPE:
  435.       if (buffer.nfields == 0)
  436.         buffer.code = TYPE_CODE_ARRAY;
  437.       else
  438.         buffer.code = TYPE_CODE_PASCAL_ARRAY;
  439.       break;
  440.  
  441.     case RECORD_TYPE:
  442.       buffer.code = TYPE_CODE_STRUCT;
  443.       break;
  444.  
  445.     case UNION_TYPE:
  446.       buffer.code = TYPE_CODE_UNION;
  447.       break;
  448.  
  449.     case FUNCTION_TYPE:
  450.       buffer.code = TYPE_CODE_FUNC;
  451.       break;
  452.  
  453.     case ENUMERAL_TYPE:
  454.       buffer.code = TYPE_CODE_ENUM;
  455.       break;
  456.  
  457.     default:
  458.       abort ();
  459.     }
  460.  
  461.       fwrite (&buffer, sizeof buffer, 1, symfile);
  462.  
  463.       /* Now write the `struct field's that certain kinds of type have.
  464.      This allocates space for the names of those fields,
  465.      incrementing next_address for the names.  */
  466.  
  467.       switch (TREE_CODE (next))
  468.     {
  469.     case ARRAY_TYPE:
  470.       if (buffer.nfields)
  471.         symout_array_domain (next);
  472.       break;
  473.  
  474.     case RECORD_TYPE:
  475.     case UNION_TYPE:
  476.       symout_record_fields (next);
  477.       break;
  478.  
  479.     case ENUMERAL_TYPE:
  480.       symout_enum_values (next);
  481.       break;
  482.  
  483.     case INTEGER_TYPE:
  484.       if (buffer.nfields)
  485.         symout_range_bounds (next);
  486.     }
  487.     }
  488.  
  489.   /* Now output the strings referred to by the fields of certain types.
  490.      (next_address was already updated for these strings.)  */
  491.  
  492.   for (next = types, i = 0;
  493.        next;
  494.        next = TREE_CHAIN (next), i++)
  495.     {
  496.       switch (TREE_CODE (next))
  497.     {
  498.     case RECORD_TYPE:
  499.     case UNION_TYPE:
  500.       symout_record_field_names (next);
  501.       break;
  502.  
  503.     case ENUMERAL_TYPE:
  504.       symout_enum_value_names (next);
  505.       break;
  506.     }
  507.     }
  508. }
  509.  
  510. /* Given a list of types TYPES, return a chain of just those
  511.    that haven't been written in the symbol table.  */
  512.  
  513. static tree
  514. filter_undefined_types (types)
  515.      tree types;
  516. {
  517.   tree new = 0;
  518.   tree next;
  519.  
  520.   for (next = types; next; next = TREE_CHAIN (next))
  521.     if (TYPE_SYMTAB_ADDRESS (TREE_PURPOSE (next)) == 0)
  522.       {
  523.     TREE_CHAIN (TREE_PURPOSE (next)) = new;
  524.     new = TREE_PURPOSE (next);
  525.       }
  526.  
  527.   return new;
  528. }
  529.  
  530. /* Return nonzero if TYPE's range of possible values
  531.    is not the full range allowed by the number of bits it has.
  532.    TYPE is assumed to be an INTEGER_TYPE or ENUMERAL_TYPE.  */
  533.  
  534. static int
  535. subrange_p (type)
  536.      tree type;
  537. {
  538.   int uns = TREE_UNSIGNED (type);
  539.  
  540.   if (TYPE_PRECISION (type) >= HOST_BITS_PER_INT)
  541.     {
  542.       if (uns)
  543.     return integer_zerop (TYPE_MIN_VALUE (type))
  544.       && TREE_INT_CST_LOW (TYPE_MAX_VALUE (type)) == 0
  545.         && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (type))
  546.         == (1 << (TYPE_PRECISION (type) - HOST_BITS_PER_INT)) - 1);
  547.       return TREE_INT_CST_LOW (TYPE_MIN_VALUE (type)) == 0
  548.     && TREE_INT_CST_LOW (TYPE_MAX_VALUE (type)) == 0
  549.       && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type))
  550.           == (-1) << (TYPE_PRECISION (type) - 1 - HOST_BITS_PER_INT))
  551.         && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (type))
  552.         == (1 << (TYPE_PRECISION (type) - 1 - HOST_BITS_PER_INT)) - 1);
  553.     }
  554.  
  555.   if (uns)
  556.     {
  557.       int mask;
  558.  
  559.       if (TYPE_PRECISION (type) == HOST_BITS_PER_INT)
  560.     /* Shifting by 32 loses on some machines.  */
  561.     mask = -1;
  562.       else
  563.     mask = (1 << TYPE_PRECISION (type)) - 1;
  564.  
  565.       return (integer_zerop (TYPE_MIN_VALUE (type))
  566.           && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (type)) == mask));
  567.     }
  568.   else
  569.     return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (type))
  570.          == (-1) << (TYPE_PRECISION (type) - 1))
  571.         && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (type))
  572.         == (1 << (TYPE_PRECISION (type) - 1)) - 1));
  573. }
  574.  
  575. /* Functions to output the "fields" of various kinds of types.
  576.    These assume that next_address has already been incremented to
  577.    cover these fields, and the fields of all the other types being
  578.    output in this batch; so next_address can be used to allocate
  579.    space to store field names, etc.  */
  580.  
  581. static void
  582. symout_array_domain (type)
  583.      tree type;
  584. {
  585.   struct field buffer;
  586.  
  587.   buffer.bitpos = 0;
  588.   buffer.bitsize = 0;
  589.   buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (TYPE_DOMAIN (type));
  590.   buffer.name = 0;
  591.   fwrite (&buffer, sizeof (struct field), 1, symfile);
  592. }
  593.  
  594. static void
  595. symout_range_bounds (type)
  596.      tree type;
  597. {
  598.   struct field buffer;
  599.  
  600.   buffer.bitpos = TREE_INT_CST_LOW (TYPE_MIN_VALUE (type));
  601.   buffer.bitsize = 0;
  602.   buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (type);
  603.   buffer.name = 0;
  604.   fwrite (&buffer, sizeof (struct field), 1, symfile);
  605.  
  606.   buffer.bitpos = TREE_INT_CST_LOW (TYPE_MAX_VALUE (type));
  607.   buffer.bitsize = 0;
  608.   buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (type);
  609.   buffer.name = 0;
  610.   fwrite (&buffer, sizeof (struct field), 1, symfile);
  611. }
  612.  
  613. static void
  614. symout_record_fields (type)
  615.      tree type;
  616. {
  617.   struct field buffer;
  618.   register tree field;
  619.  
  620.   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  621.     {
  622.       buffer.bitpos = DECL_OFFSET (field);
  623.       buffer.bitsize
  624.     = (TREE_PACKED (field)
  625.        ? TREE_INT_CST_LOW (DECL_SIZE (field)) * DECL_SIZE_UNIT (field)
  626.        : 0);
  627.       buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (TREE_TYPE (field));
  628.       if (DECL_NAME (field))
  629.     {
  630.       buffer.name = (char *) next_address;
  631.       symout_strings_skip (0, IDENTIFIER_LENGTH (DECL_NAME (field)), 0, 0);
  632.     }
  633.       else
  634.     buffer.name = 0;
  635.       fwrite (&buffer, sizeof (struct field), 1, symfile);
  636.     }
  637. }
  638.  
  639. static void
  640. symout_enum_values (type)
  641.      tree type;
  642. {
  643.   struct field buffer;
  644.   register tree link, value;
  645.  
  646.   for (link = TYPE_VALUES (type); link; link = TREE_CHAIN (link))
  647.     {
  648.       value = TREE_VALUE (link);
  649.       buffer.bitpos = TREE_INT_CST_LOW (value);
  650.       buffer.bitsize = 0;
  651.       buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (type);
  652.       buffer.name = (char *) next_address;
  653.       symout_strings_skip (0, IDENTIFIER_LENGTH (TREE_PURPOSE (link)), 0, 0);
  654.       fwrite (&buffer, sizeof buffer, 1, symfile);
  655.     }
  656. }
  657.  
  658. /* Output field names or value names for the fields of a type.
  659.    This is called, for the types that need it, after the fields
  660.    have been output for all the types in the batch.
  661.    We do not update next_address here, because it has already been 
  662.    updated for all the names in all the fields in all the types.  */
  663.  
  664. static void
  665. symout_record_field_names (type)
  666.      tree type;
  667. {
  668.   register tree field;
  669.  
  670.   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  671.     if (DECL_NAME (field))
  672.       symout_strings_print (IDENTIFIER_POINTER (DECL_NAME (field)),
  673.                 IDENTIFIER_LENGTH (DECL_NAME (field)),
  674.                 0, 0);
  675. }
  676.  
  677. static void
  678. symout_enum_value_names (type)
  679.      tree type;
  680. {
  681.   register tree value;
  682.  
  683.   for (value = TYPE_VALUES (type); value; value = TREE_CHAIN (value))
  684.     symout_strings_print (IDENTIFIER_POINTER (TREE_PURPOSE (value)),
  685.               IDENTIFIER_LENGTH (TREE_PURPOSE (value)),
  686.               0, 0);
  687. }
  688.  
  689. /* Output the symbols of a block, given the list of decl nodes.
  690.    Store the file addresses at which the symbols are output
  691.    into ADDR_BUFFER, a vector which has just the right length.
  692.  
  693.    If FILTER is 1, do only the private symbols in DECLS.
  694.    If FILTER is 2, do only the public ones (but no externals).
  695.    If FILTER is 0, do all (except external functions).  */
  696.  
  697. static void
  698. symout_block_symbols (decls, addr_buffer, filter)
  699.      tree decls;
  700.      int *addr_buffer;
  701.      int filter;
  702. {
  703.   register tree decl;
  704.   struct symbol buffer;
  705.   register int i;
  706.  
  707.   for (decl = decls, i = 0; decl; decl = TREE_CHAIN (decl))
  708.     {
  709.       register name_address = next_address;
  710.  
  711.       if (filter == (TREE_PUBLIC (decl) ? 1 : 2))
  712.     continue;
  713.  
  714.       /* Do not mention external functions.
  715.      Let their own files mention them.
  716.      In the top blocks, don't mention external anything.  */
  717.  
  718.       if (TREE_EXTERNAL (decl)
  719.       && (filter || TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE))
  720.     continue;
  721.  
  722.       if (TREE_TYPE (decl) == error_mark_node)
  723.     continue;
  724.  
  725.       symout_strings (IDENTIFIER_POINTER (DECL_NAME (decl)),
  726.               IDENTIFIER_LENGTH (DECL_NAME (decl)),
  727.               0, 0);
  728.       addr_buffer[i] = next_address;
  729.       buffer.name = (char *) name_address;
  730.       buffer.namespace = VAR_NAMESPACE;
  731.       buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (TREE_TYPE (decl));
  732.       switch (TREE_CODE (decl))
  733.     {
  734.     case PARM_DECL:
  735.       buffer.class = LOC_ARG;
  736.       buffer.value.value = DECL_OFFSET (decl) / BITS_PER_UNIT;
  737.       break;
  738.  
  739.     case VAR_DECL:
  740.     case RESULT_DECL:
  741.       if (TREE_STATIC (decl) || TREE_EXTERNAL (decl))
  742.         {
  743.           if (! TREE_PUBLIC (decl) || DECL_INITIAL (decl))
  744.         {
  745.           char *str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
  746.           fprintf (asmfile, "\t.gdbsym ");
  747.           ASM_OUTPUT_LABELREF (asmfile, str);
  748.           fprintf (asmfile, ",%d\n",
  749.                next_address + (char *)&buffer.value - (char *)&buffer);
  750.           buffer.class = LOC_STATIC;
  751.         }
  752.           else
  753.         /* Uninitialized public symbols are output as .comm;
  754.            Tell GDB to get address from loader global symbol.
  755.            Also come here for symbols declared extern.  */
  756.         buffer.class = LOC_EXTERNAL;
  757.         }
  758.       else
  759.         {
  760.           if (GET_CODE (DECL_RTL (decl)) == REG)
  761.         {
  762.           buffer.class = LOC_REGISTER;
  763.           buffer.value.value = REGNO (DECL_RTL (decl));
  764.           /* Detect vars that were optimized entirely away.  */
  765.           if (buffer.value.value == -1)
  766.             buffer.class = LOC_CONST;
  767.         }
  768.           /* Locals in memory are expected to be addressed as
  769.          (PLUS (REG ...) (CONST_INT ...)).
  770.          Bomb out if that is not so.  */
  771.           else if (GET_CODE (DECL_RTL (decl)) == MEM)
  772.         {
  773.           register rtx addr = XEXP (DECL_RTL (decl), 0);
  774.           if (GET_CODE (addr) != PLUS && GET_CODE (addr) != MINUS)
  775.             abort ();
  776.           if (GET_CODE (XEXP (addr, 1)) != CONST_INT)
  777.             abort ();
  778.           buffer.class = LOC_LOCAL;
  779.           buffer.value.value = INTVAL (XEXP (addr, 1));
  780.           if (GET_CODE (addr) == MINUS)
  781.             buffer.value.value = - buffer.value.value;
  782.         }
  783.           else
  784.         abort ();
  785.         }
  786.       break;
  787.  
  788.     case TYPE_DECL:
  789.       buffer.class = LOC_TYPEDEF;
  790.       buffer.value.value = 0;
  791.       break;
  792.  
  793.     case CONST_DECL:
  794.       buffer.class = LOC_CONST;
  795.       buffer.value.value = TREE_INT_CST_LOW (DECL_INITIAL (decl));
  796.       break;
  797.  
  798.     case FUNCTION_DECL:
  799.       if (DECL_INITIAL (decl))
  800.         {
  801.           buffer.class = LOC_BLOCK;
  802.           buffer.value.value = DECL_BLOCK_SYMTAB_ADDRESS (decl);
  803.         }
  804.       else
  805.         buffer.class = LOC_EXTERNAL;
  806.     }
  807.  
  808.       fwrite (&buffer, sizeof buffer, 1, symfile);
  809.       next_address += sizeof buffer;
  810.       i++;
  811.     }
  812. }
  813.  
  814. /* Output the tags (struct, union and enum definitions) for a block,
  815.    given a list of them (a chain of TREE_LIST nodes) in TAGS.
  816.    Store their addresses in the file into ADDR_BUFFER.  */
  817.  
  818. static void
  819. symout_block_tags (tags, addr_buffer)
  820.      tree tags;
  821.      int *addr_buffer;
  822. {
  823.   register tree tag;
  824.   struct symbol buffer;
  825.   register int i;
  826.  
  827.   for (tag = tags, i = 0; tag; tag = TREE_CHAIN (tag), i++)
  828.     {
  829.       buffer.name = (char *) next_address;
  830.  
  831.       symout_strings (IDENTIFIER_POINTER (TREE_PURPOSE (tag)),
  832.               IDENTIFIER_LENGTH (TREE_PURPOSE (tag)),
  833.               0, 0);
  834.       addr_buffer[i] = next_address;
  835.       buffer.namespace = STRUCT_NAMESPACE;
  836.       buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (TREE_VALUE (tag));
  837.       buffer.class = LOC_TYPEDEF;
  838.       buffer.value.value = 0;
  839.  
  840.       fwrite (&buffer, sizeof buffer, 1, symfile);
  841.       next_address += sizeof buffer;
  842.     }
  843. }
  844.  
  845. /* Output all the data structure for a "block"
  846.    (any binding contour).
  847.    DECLS is the chain of declarations of variables in this block.
  848.    TAGS is the list of struct, union and enum tag definitions of this block.
  849.    SUPERBLOCK_ADDRESS is the symtab file address of the containing block's
  850.    data structure.  */
  851.  
  852. int
  853. symout_block (decls, tags, args, superblock_address)
  854.      tree decls;
  855.      tree tags;
  856.      tree args;
  857.      int superblock_address;
  858. {
  859.   register tree decl;
  860.   register int i;
  861.   register int *addr_buffer;
  862.   struct block buffer;
  863.   int n_decls, n_tags, n_args, total;
  864.   register struct blockvec_elt *velt;
  865.   int block_address;
  866.  
  867.   for (decl = decls, i = 0; decl; decl = TREE_CHAIN (decl))
  868.     if (! TREE_EXTERNAL (decl)
  869.     || TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE)
  870.       i++;
  871.  
  872.   n_decls = i;
  873.  
  874.   for (decl = args, i = 0; decl; decl = TREE_CHAIN (decl), i++);
  875.   n_args = i;
  876.  
  877.   for (decl = tags, i = 0; decl; decl = TREE_CHAIN (decl), i++);
  878.   n_tags = i;
  879.  
  880.   total = n_decls + n_args + n_tags;
  881.  
  882.   addr_buffer = (int *) alloca (total * sizeof (int));
  883.  
  884.   symout_block_symbols (args, addr_buffer, 0);
  885.   symout_block_symbols (decls, addr_buffer + n_args, 0);
  886.   symout_block_tags (tags, addr_buffer + n_decls + n_args);
  887.  
  888.   velt = (struct blockvec_elt *) xmalloc (sizeof (struct blockvec_elt));
  889.   velt->next = blockvec;
  890.   velt->address = next_address;
  891.   blockvec = velt;
  892.  
  893.   buffer.startaddr = 0;
  894.   buffer.endaddr = 0;
  895.   buffer.superblock = (struct block *) superblock_address;
  896.   buffer.function = 0;
  897.   buffer.nsyms = total;
  898.  
  899.   block_address = next_address;
  900.   fwrite (&buffer, sizeof buffer - sizeof buffer.sym, 1, symfile);
  901.   next_address += sizeof buffer - sizeof buffer.sym;
  902.  
  903.   fwrite (addr_buffer, sizeof (int), total, symfile);
  904.   next_address += total * sizeof (int);
  905.  
  906.   fprintf (asmfile, "\t.gdbblock %d,%d\n", total_blocks + 2, block_address);
  907.   total_blocks++;
  908.  
  909.   return block_address;
  910. }
  911.  
  912. /* Walk STMT, the body of a function, and output symtab data on
  913.    all the blocks that compose it and all symbols inside them.
  914.    ARGS is a chain of decls for argument variables of the function.
  915.    SUPERBLOCK_ADDRESS is the address of symbol data for the
  916.    innermost block containing STMT; it is used for recursive calls,
  917.    and is always 0 for the outermost call (since the containing
  918.    block for a function is output later than the function).  */
  919.  
  920. int
  921. symout_function (stmt, args, superblock_address)
  922.      register tree stmt;
  923.      tree args;
  924.      int superblock_address;
  925. {
  926.   int address = superblock_address;
  927.  
  928.   while (stmt)
  929.     {
  930.       switch (TREE_CODE (stmt))
  931.     {
  932.     case COMPOUND_STMT:
  933.     case LOOP_STMT:
  934.       symout_function (STMT_BODY (stmt), 0, address);
  935.       break;
  936.  
  937.     case IF_STMT:
  938.       symout_function (STMT_THEN (stmt), 0, address);
  939.       symout_function (STMT_ELSE (stmt), 0, address);
  940.       break;
  941.  
  942.     case LET_STMT:
  943.       address =
  944.         symout_block (STMT_VARS (stmt), STMT_TYPE_TAGS (stmt), args,
  945.               superblock_address);
  946.  
  947.       symout_function (STMT_BODY (stmt), 0, address);
  948.     }
  949.       stmt = TREE_CHAIN (stmt);
  950.     }
  951.   return address;
  952. }
  953.  
  954. symout_function_end ()
  955. {
  956.   /* Output dummy entries for any undefined structure references.  */
  957.   symout_types (filter_undefined_types (temporary_fwd_refs));
  958.   temporary_fwd_refs = 0;
  959. }
  960.  
  961. /* Output all the data structure for a top two blocks in a compilation.
  962.    The top block is for public (global) symbols;
  963.    the next one is for private (this file only) symbols.
  964.  
  965.    DECLS is the chain of declarations of variables in this block.
  966.    TAGS is the list of struct, union and enum tag definitions.  */
  967.  
  968. void
  969. symout_top_blocks (decls, tags)
  970.      tree decls;
  971.      tree tags;
  972. {
  973.   register tree decl;
  974.   register int i;
  975.   register int *addr_buffer;
  976.   struct block buffer;
  977.   int n_decls, n_tags;
  978.   register struct blockvec_elt *velt;
  979.   int top_block_addr;
  980.  
  981.   /* First do the public-symbols block.  */
  982.  
  983.   for (decl = decls, i = 0; decl; decl = TREE_CHAIN (decl))
  984.     if (TREE_PUBLIC (decl) && ! TREE_EXTERNAL (decl))
  985.       i++;
  986.   n_decls = i;
  987.  
  988.   addr_buffer = (int *) alloca (n_decls * sizeof (int));
  989.  
  990.   symout_block_symbols (decls, addr_buffer, 2);
  991.  
  992.   fprintf (asmfile, ".text 0\n\t.gdbend 0\n");
  993.   fprintf (asmfile, "\t.gdbblock 0,%d\n", next_address);
  994.  
  995.   total_blocks++;
  996.   velt = (struct blockvec_elt *) xmalloc (sizeof (struct blockvec_elt));
  997.   velt->next = blockvec;
  998.   velt->address = next_address;
  999.   blockvec = velt;
  1000.  
  1001.   top_block_addr = next_address;
  1002.  
  1003.   buffer.startaddr = 0;
  1004.   buffer.endaddr = 0;
  1005.   buffer.superblock = 0;
  1006.   buffer.function = 0;
  1007.   buffer.nsyms = n_decls;;
  1008.  
  1009.   fwrite (&buffer, sizeof buffer - sizeof buffer.sym, 1, symfile);
  1010.   next_address += sizeof buffer - sizeof buffer.sym;
  1011.  
  1012.   fwrite (addr_buffer, sizeof (int), n_decls, symfile);
  1013.   next_address += n_decls * sizeof (int);
  1014.  
  1015.   /* Next do the private-symbols block.  */
  1016.  
  1017.   for (decl = decls, i = 0; decl; decl = TREE_CHAIN (decl))
  1018.     if (! TREE_PUBLIC (decl) && ! TREE_EXTERNAL (decl))
  1019.       i++;
  1020.   n_decls = i;
  1021.  
  1022.   for (decl = tags, i = 0; decl; decl = TREE_CHAIN (decl), i++);
  1023.   n_tags = i;
  1024.  
  1025.   addr_buffer = (int *) alloca ((n_decls + n_tags) * sizeof (int));
  1026.  
  1027.   symout_block_symbols (decls, addr_buffer, 1);
  1028.   symout_block_tags (tags, addr_buffer + n_decls);
  1029.  
  1030.   fprintf (asmfile, "\t.gdbend 1\n");
  1031.   fprintf (asmfile, "\t.gdbblock 1,%d\n", next_address);
  1032.  
  1033.   total_blocks++;
  1034.   velt = (struct blockvec_elt *) xmalloc (sizeof (struct blockvec_elt));
  1035.   velt->next = blockvec;
  1036.   velt->address = next_address;
  1037.   blockvec = velt;
  1038.  
  1039.   buffer.startaddr = 0;
  1040.   buffer.endaddr = 0;
  1041.   buffer.superblock = (struct block *) top_block_addr;
  1042.   buffer.function = 0;
  1043.   buffer.nsyms = n_decls + n_tags;;
  1044.  
  1045.   fwrite (&buffer, sizeof buffer - sizeof buffer.sym, 1, symfile);
  1046.   next_address += sizeof buffer - sizeof buffer.sym;
  1047.  
  1048.   fwrite (addr_buffer, sizeof (int), n_decls + n_tags, symfile);
  1049.   next_address += (n_decls + n_tags) * sizeof (int);
  1050. }
  1051.  
  1052. /* Output the source-line-number information.  */
  1053.  
  1054. /* Output a `struct source' for the source file described by F.
  1055.    Return the address-in-the-symseg of the `struct source'.  */
  1056.  
  1057. static int
  1058. symout_source_file (f)
  1059.      struct gdbfile *f;
  1060. {
  1061.   /* Make the `struct source' big enough for as many lines as
  1062.      this file has.  */
  1063.   int size = sizeof (struct source) + (f->nlines - 1) * sizeof (struct line);
  1064.   struct source *buffer
  1065.     = (struct source *) alloca (size);
  1066.   int addr;
  1067.  
  1068.   /* Use zero for the line data, since assembler will store the real data.  */
  1069.   bzero (buffer, size);
  1070.  
  1071.   /* Output the file's name as a string.  The assembler doesn't know this.  */
  1072.   buffer->name = (char *) next_address;
  1073.   symout_strings (f->name, 0, 0, 0);
  1074.   buffer->nlines = f->nlines;
  1075.  
  1076.   /* Write the structure.  */
  1077.   addr = next_address;
  1078.   fwrite (buffer, 1, size, symfile);
  1079.   next_address += size;
  1080.  
  1081.   /* Tell assembler where to write the real line-number data.  */
  1082.   fprintf (asmfile, "\t.gdblinetab %d,%d\n",
  1083.        f->filenum, addr + sizeof (int));
  1084.  
  1085.   return addr;
  1086. }
  1087.  
  1088. /* Output the `struct sourcevector' which describes all the
  1089.    source files and points a `struct source' for each one.  */
  1090.  
  1091. static int
  1092. symout_sources ()
  1093. {
  1094.   register struct gdbfile *f;
  1095.   int nfiles = 0;
  1096.   struct sourcevector *s;
  1097.   int i;
  1098.   int size;
  1099.   int addr;
  1100.  
  1101.   /* Count number of files to determine size of the sourcevector.  */
  1102.   for (f = gdbfiles; f; f = f->next)
  1103.     ++nfiles;
  1104.  
  1105.   /* Allocate buffer for the sourcevector and record its length.  */
  1106.   size = sizeof (int) + nfiles * sizeof (struct source *);
  1107.   s = (struct sourcevector *) alloca (size);
  1108.   s->length = nfiles;
  1109.  
  1110.   /* Output a `struct source' for each file; put address into sourcevector.  */
  1111.   for (f = gdbfiles, i = 0; f; f = f->next, i++)
  1112.     s->source[i] = (struct source *) symout_source_file (f);
  1113.  
  1114.   /* Output the sourcevector.  */
  1115.   addr = next_address;
  1116.   fwrite (s, 1, size, symfile);
  1117.   next_address += size;
  1118.   return addr;
  1119. }
  1120.  
  1121. /* Call here at the end of compilation, after outputting all the
  1122.    blocks and symbols, to output the blockvector and typevector
  1123.    and close the symbol table file.  FILETIME is source file's
  1124.    creation time.  */
  1125.  
  1126. void
  1127. symout_finish (filename, filetime)
  1128.      char *filename;
  1129.      int filetime;
  1130. {
  1131.   int *blockvector = (int *) alloca ((total_blocks + 1) * sizeof (int));
  1132.   int *typevector = (int *) alloca ((total_types + 1) * sizeof (int));
  1133.   int now = time (0);
  1134.   register int i;
  1135.   struct symbol_root buffer;
  1136.   char dir[MAXNAMLEN];
  1137.  
  1138.   /* Output dummy entries for any undefined structure references.  */
  1139.   symout_types (filter_undefined_types (permanent_fwd_refs));
  1140.  
  1141.   buffer.language = language_c;
  1142.   buffer.blockvector = (struct blockvector *) next_address;
  1143.  
  1144.   /* The two blocks at the beginning of the chain
  1145.      are the file's private symbols block and public symbols block.
  1146.      They belong at the front of the blockvector, in that order.  */
  1147.   blockvector[2] = blockvec->address;
  1148.   blockvec = blockvec->next;
  1149.   blockvector[1] = blockvec->address;
  1150.   blockvec = blockvec->next;
  1151.  
  1152.   /* The rest of the blocks are in the chain in reverse order.  */
  1153.   for (i = total_blocks; i > 2; i--)
  1154.     {
  1155.       blockvector[i] = blockvec->address;
  1156.       blockvec = blockvec->next;
  1157.     }
  1158.   blockvector[0] = total_blocks;
  1159.  
  1160.   fwrite (blockvector, sizeof (int), total_blocks + 1, symfile);
  1161.   next_address += sizeof (int) * (total_blocks + 1);
  1162.  
  1163.   buffer.typevector = (struct typevector *) next_address;
  1164.  
  1165.   for (i = total_types; i > 0; i--)
  1166.     {
  1167.       typevector[i] = typevec->address;
  1168.       typevec = typevec->next;
  1169.     }
  1170.   typevector[0] = total_types;
  1171.  
  1172.   fwrite (typevector, sizeof (int), total_types + 1, symfile);
  1173.   next_address += sizeof (int) * (total_types + 1);
  1174.  
  1175.   buffer.sourcevector = (struct sourcevector *) symout_sources ();
  1176.  
  1177.   buffer.format = 1;
  1178.   buffer.textrel = 0;        /* These four will be set up by linker.  */
  1179.   buffer.datarel = 0;        /* Make them 0 now, which is right for */
  1180.   buffer.bssrel = 0;        /* looking at the .o file in gdb.  */
  1181.   buffer.ldsymoff = 0;
  1182.  
  1183.   buffer.version = (char *) next_address;
  1184.   symout_strings (ctime (&filetime), 0, 0, 0);
  1185.  
  1186.   buffer.compilation = (char *) next_address;
  1187.   symout_strings (ctime (&now), 0, 0, 0);
  1188.  
  1189.   buffer.filename = (char *) next_address;
  1190.   symout_strings (filename, 0, 0, 0);
  1191.  
  1192.   buffer.filedir = (char *) next_address;
  1193. #ifdef USG
  1194.   strcpy (dir, getcwd (dir, MAXNAMLEN));
  1195. #else
  1196. #ifndef VMS
  1197.   getwd (dir);
  1198. #else
  1199.   abort ();
  1200. #endif
  1201. #endif
  1202.   symout_strings (dir, 0, 0, 0);
  1203.  
  1204.   fflush (symfile);
  1205.  
  1206.   if (ferror (symfile) != 0)
  1207.     fatal_io_error (symfile_name);
  1208.  
  1209.   buffer.length = next_address;
  1210.  
  1211.   if (lseek (fileno (symfile), 0, 0) < 0)
  1212.     pfatal_with_name (symfile_name);
  1213.   if (write (fileno (symfile), &buffer, sizeof buffer) < 0)
  1214.     pfatal_with_name (symfile_name);
  1215.   close (fileno (symfile));
  1216. }
  1217.